package com.buric.auth.controller;

import com.buric.api.dto.admin.LoginAdmin;
import com.buric.auth.service.AuthAdminService;
import com.buric.auth.service.CommonService;
import com.buric.auth.service.data.in.LoginInData;
import com.buric.common.data.R;
import com.buric.common.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author: zhou.wei
 * @description 认证相关接口
 * @date: 2024/5/10 11:23
 */

@Api(value = "管理员认证模块", tags = "认证模块")
@RestController
@RequestMapping("/authAdmin")
public class AuthAdminController {

    @Resource
    private AuthAdminService authAdminService;

    @Resource
    private CommonService commonService;


    @ApiOperation("获取验证码")
    @GetMapping("captcha")
    public R<String> getCaptcha(@RequestParam("captchaId")String captchaId) {
        // 生成验证码
        String captcha = commonService.getCaptcha(captchaId, 120L);
        // 验证码信息
        return R.ok(captcha);
    }

    @ApiOperation("登录")
    @PostMapping("login")
    public R<LoginAdmin> login(@Validated @RequestBody LoginInData inData) {

        boolean b = commonService.checkCaptcha(inData.getCaptcha(), inData.getCaptchaId());
        if(!b){
            throw new ServiceException("验证码错误");
        }
        // 用户登录
        LoginAdmin userInfo = authAdminService.login(inData);
        // 获取登录token
        return R.ok(userInfo);
    }




}
